/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
package view.servlets;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import model.Vocabulary;

import org.apache.log4j.Logger;
import org.openrdf.model.URI;
import org.openrdf.model.impl.URIImpl;

import controller.HelperFunctions;
import controller.elementMapper.ClassMapper;
import controller.elementMapper.Mapper;
import controller.elementMapper.PropertyMapper;

public class ExportHelper {
	

	// Authenticates the Resources URIs
	// (the context has to be set beween base and the marker)
	private static final String RESOURCE_URI_MARKER = "/id";
	public static final String BASE = "http://myontology.deri.at";
	public static String context = "";
	
public static Logger logger = Logger.getLogger(ExportHelper.class);
	
	public static String getHeader( String cont ) {
		
		ExportHelper.context = cont;
		
		String ret = 
			"<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n" +
    		"<rdf:RDF\n" +
    		// "	xmlns=\"" + BASE + response.encodeURL( id.stringValue() ) + RESOURCE_URI_ENDING + "\"\n" +
    		
    		"	xmlns:xsd=\"http://www.w3.org/2001/XMLSchema#\"\n" +
    		"	xmlns:foaf=\"http://xmlns.com/foaf/0.1/\"\n" +
    		"	xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n" +
    		"	xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\"\n" +
    		"	xmlns:owl=\"http://www.w3.org/2002/07/owl#\"\n" +
    		"	xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n" +

    		"	xmlns:mo=\"" + BASE + context + RESOURCE_URI_MARKER + "/\"\n" + 
    		
    		"	xml:base=\"" + BASE + "\" >\n";

		
	    ret += "	<owl:AnnotationProperty rdf:about=\"" + BASE + context + RESOURCE_URI_MARKER + "/htmlDocumentation\"/>\n";
	    ret += "	<owl:AnnotationProperty rdf:about=\"" + BASE + context + RESOURCE_URI_MARKER + "/flickr\"/>\n";
	    ret += "	<owl:AnnotationProperty rdf:about=\"" + BASE + context + RESOURCE_URI_MARKER + "/youtube\"/>\n";
	    ret += "	<owl:AnnotationProperty rdf:about=\"" + BASE + context + RESOURCE_URI_MARKER + "/htmlDocumentation\"/>\n";
	    ret += "<owl:AnnotationProperty rdf:about=\"http://purl.org/dc/elements/1.1/rights\"/>";
	   
		return ret;
	}
	
	
	
	public static String getOntologyModule( URI id ) {
		String ret = "";
		// BASE + context + "/" + response.encodeURL( id.stringValue() )
		try {
		    ret +=
	    		"	<owl:Ontology rdf:about=\"" + BASE + context + "/" + HelperFunctions.getUriForHref( id ) + RESOURCE_URI_MARKER + "\">\n";
		    ret += "<dc:rights>This ontology is available under a Creative Commons Attribution 3.0 license, see http://creativecommons.org/licenses/by/3.0/. Proper attribuition is met by a link to http://www.myontology.org. Information on contributors given in the header must not be removed.</dc:rights>";
			ret += ExportHelper.getGeneralElements( id, Vocabulary.ENGLISH );
	
			//ret += 
			//	"		<dc:creator>http://myontology.deri.at/</dc:creator>\n";
				    
		    ret +=
	    		"	</owl:Ontology>\n";
	    	
		} catch( Exception e ) {
			logger.error(e);
			ret = "";
		}
    	
    	return ret;		
	}
	
	
	public static String getClass( URI id ) {

		String ret = "";
		
		try {
		    ret +=
	    		"	<owl:Class rdf:about=\"" + BASE + context + "/" + HelperFunctions.getUriForHref( id ) + RESOURCE_URI_MARKER + "\">\n";

			ret += ExportHelper.getGeneralElements( id, Vocabulary.ENGLISH );

		    ret += ExportHelper.getRdfResources( "owl:equivalentClass", ClassMapper.selectSameAs(id) );		
		    ret += ExportHelper.getRdfResources( "rdfs:subClassOf", ClassMapper.selectSubclassOf(id) );		   
		    ret += ExportHelper.getRdfResourcesFromStringList("mo:youtube", ClassMapper.selectVideos(id),Vocabulary.YOUTUBEVIDEO);
		    ret += ExportHelper.getRdfResourcesFromStringList("mo:flickr", ClassMapper.selectImages(id),Vocabulary.FLICKRIMAGE);
			
			//ret += 
			//	"		<dc:creator>http://myontology.deri.at/</dc:creator>\n";
		    
	    	ret +=	
	    		"	</owl:Class>\n";
	    	
		} catch( Exception e ) {
			logger.error(e);
			ret = "";
		}
    	
    	return ret;		
	}
	

	public static String getDatatypeProperty( URI id ) {

		String ret = "";

		try {
		    ret +=
	    		"	<owl:DatatypeProperty rdf:about=\"" + BASE + context + "/" + HelperFunctions.getUriForHref( id ) + RESOURCE_URI_MARKER + "\">\n";

			ret += ExportHelper.getGeneralElements( id, Vocabulary.ENGLISH );
			
		    ret += ExportHelper.getRdfResources( "owl:equivalentProperty", PropertyMapper.selectSameAs(id) );			
		    ret += ExportHelper.getRdfResources( "rdfs:domain", PropertyMapper.selectDomain(id) );	
		    ret += ExportHelper.getRdfResourcesDTPRange( PropertyMapper.selectRange(id) );
		    
		    
			//ret += 
			//	"		<dc:creator>http://myontology.deri.at/</dc:creator>\n";
			
	    	ret +=	
	    		"	</owl:DatatypeProperty>\n";

		} catch( Exception e ) {
			logger.error(e);
			ret = "";
		}
    	
    	return ret;		
	}


	public static String getObjectProperty( URI id ) {

		String ret = "";


		try {
			
		    ret +=
	    		"	<owl:ObjectProperty rdf:about=\"" + BASE + context + "/" + HelperFunctions.getUriForHref( id ) + RESOURCE_URI_MARKER + "\">\n";
	
			ret += ExportHelper.getGeneralElements( id, Vocabulary.ENGLISH );

		    ret += ExportHelper.getRdfResources( "owl:equivalentProperty", PropertyMapper.selectSameAs(id) );		
		    ret += ExportHelper.getRdfResources( "rdfs:domain", PropertyMapper.selectDomain(id) );    
		    ret += ExportHelper.getRdfResources( "rdfs:range", PropertyMapper.selectRange(id) );
	
			//ret += 
			//	"		<dc:creator>http://myontology.deri.at/</dc:creator>\n";
			
	    	ret +=	
	    		"	</owl:ObjectProperty>\n";

		} catch( Exception e ) {
			logger.error(e);
			ret = "";
		}
    	
    	return ret;		
	}

	public static String getGeneralElements( URI id, String lang ) throws Exception {

		String ret = "";		

		ret += ExportHelper.getReferenceToHtmlPage( BASE + context + "/" + HelperFunctions.getUriForHref( id ) );
		ret += ExportHelper.getReferenceToRdfPage( BASE + context + "/" + HelperFunctions.getUriForHref( id ) );

	    ret += ExportHelper.getRdfLabel( Mapper.selectLabel( id, Vocabulary.ENGLISH ) );
	    ret += ExportHelper.getRdfComment( Mapper.selectComment( id, Vocabulary.ENGLISH ) );
		ret += ExportHelper.getRdfSeeAlso( Mapper.selectSeeAlso( id ) );

    	return ret;	
	}

	public static String getCreator() {
		String ret = "";

		ret += 
			"		<dc:creator>http://myontology.deri.at/</dc:creator>\n";
		
		return ret;
	}

	public static String getReferenceToHtmlPage( String entry ) {
		String ret = "";

		if( entry != null ) {
			ret += 
	    		"		<mo:htmlDocumentation rdf:resource=\"" + entry + "\"/>\n";
		}
		
		return ret;
	}
	
	public static String getReferenceToRdfPage( String entry ) {
		String ret = "";

		if( entry != null ) {
			ret += 
	    		"		<rdfs:isDefinedBy rdf:resource=\"" + entry + ".rdf-xml.owl\"/>\n";
		}
		
		return ret;
	}


	public static String getRdfLabel( String entry ) {
		String ret = "";

		if( entry != null ) {
			ret += 
				"		<rdfs:label xml:lang=\"en\">" + entry + "</rdfs:label>\n";
		}

		return ret;
	}
	
	public static String getRdfComment( String entry ) {
		String ret = "";

		if( entry != null ) {
			ret += 
				"		<rdfs:comment xml:lang=\"en\">" + HelperFunctions.HTMLEncode(entry) + "</rdfs:comment>\n";
		}
		
		return ret;
	}
	
	
	public static String getRdfResourcesDTPRange( ArrayList<URI> list ) {
		String ret = "";

		if( list!= null ) {
		    Iterator<URI> iter = list.iterator();
		    while( iter.hasNext() ) {
	    		
	    		ret += 
	    			"		<rdfs:range rdf:resource=\"" + iter.next().stringValue() + "\" />\n";	
		    }
		}
	
		
		return ret;
	}
	
	public static String getRdfResourcesFromStringList( String tag, ArrayList<String> stringlist,URI prefix ) {
		if (stringlist==null) return "";
		List<URI> list = new ArrayList<URI>();
		for (Iterator<String> it = stringlist.iterator();it.hasNext();) {
			list.add(new URIImpl(prefix+"/"+it.next()));
		}
		String ret = "";

		if( list!= null ) {
		    Iterator<URI> iter = list.iterator();
		    while( iter.hasNext() ) {
		    	
	    		ret += 
	    			"		<" + tag +">"  +HelperFunctions.getUriForHref( iter.next() )+"</"+tag+">" ;	
		    }
		}
	
		
		return ret;
	}
	
	public static String getRdfResources( String tag, ArrayList<URI> list ) {
		String ret = "";

		if( list!= null ) {
		    Iterator<URI> iter = list.iterator();
		    while( iter.hasNext() ) {
		    	
	    		ret += 
	    			"		<" + tag +" rdf:resource=\"" + BASE + context + "/" + HelperFunctions.getUriForHref( iter.next() ) + RESOURCE_URI_MARKER + "\" />\n";	
		    }
		}
	
		
		return ret;
	}
	

	public static String getRdfSeeAlso( ArrayList<URI> list ) {
		String ret = "";

		if( list!= null ) {
		    Iterator<URI> iter = list.iterator();
		    while( iter.hasNext() ) {
	    		
	    		ret += 
	    			"		<rdfs:seeAlso rdf:resource=\"" + iter.next().stringValue() + "\" />\n";	
		    }
		}
	
		
		return ret;
	}
	

}
